package pri.hillchen.std.curator;

import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;

/**
 * Created by hillchen on 2018/3/12.
 */
public class CuratorClientUtil {

    private static final String CONNECTSTRING = "centos01:2182,centos01:2183,centos01:2184";
    private static final int SESSION_TIMEOUT = 20000;

    /**
     * 创建zookeeper客户端连接请求
     *
     * @return
     */
    public static CuratorFramework createCuratorFramework(){
        return createCuratorFramework(CONNECTSTRING,SESSION_TIMEOUT,SESSION_TIMEOUT);

    }

    public static CuratorFramework createCuratorFramework(String connectStr, int sessionTimeOut, int connectTimeOut){
        CuratorFramework curatorFramework = CuratorFrameworkFactory.builder()
                .connectString(connectStr)
                .connectionTimeoutMs(connectTimeOut)
                .sessionTimeoutMs(sessionTimeOut)
                .retryPolicy(new ExponentialBackoffRetry(1000,3))
                .build();
        //必须stat才可以用否则会抛出异常:
        //java.lang.IllegalStateException: instance must be started before calling this method
        curatorFramework.start();
        return curatorFramework;

    }
}
